public class LC0605 {
    public static boolean canPlaceFlowers(int[] flowerbed, int n) {
        if (n == 0) return true;

        int maxPlant = 0; // 最多能种植多少花
        int length = flowerbed.length;
        for (int i = 0; i < flowerbed.length; i++) {
            boolean slotValid = false;
            if (flowerbed[i] == 0) {
                if (i == 0 && i + 1 == length) return true; // 花坛长度为1，且刚好为空
                else if (i == 0 && i + 1 < length && flowerbed[i + 1] == 0) slotValid = true;
                else if (i == length - 1 && i - 1 >= 0 && flowerbed[i - 1] == 0) slotValid = true;
                else if (i - 1 >= 0 && flowerbed[i - 1] == 0 && i + 1 < length && flowerbed[i + 1] == 0)
                    slotValid = true;

                if (slotValid) {
                    flowerbed[i] = 1;
                    maxPlant++;
                    if (maxPlant >= n) return true;
                }
            }
        }

        return false;
    }

    public static void main(String[] args) {
        int[] flowerbed = {1, 0, 0, 0, 1};
        int n = 1;
        boolean result = canPlaceFlowers(flowerbed, n);
        System.out.println(result);
    }
}
